<?php
/**
 * 获取指定店铺的订单
 *
 * @author hzjsq@msn.com
 * @version 01.
 */
$argv[1] = 'gome.com';
$argv[2] = '2012-11-16 00:00:00';
$argv[3] = '2012-11-16 23:59:59';

$domain = $argv[1];
$sDate = time()-86400;//strtotime($argv[2]);
$eDate = time();//strtotime($argv[3]);


define('COMMAND_MODE', true);

set_time_limit(0);

define('LIB_DIR', dirname(__FILE__) . '/lib/');
require_once(LIB_DIR . 'api/taobao/order.php');
require_once(dirname(__FILE__) . '/lib/init.php');

$orderObj = new tools_api_taobao_order();


$shopList = getShopList();
//$shopList = json_decode('[{"shop_id":"0368b91c0192ba906972e0da48d07798","shop_bn":"13116935244e2edad49b","name":"\u590f\u82b1\u821e Dance","shop_type":"taobao","config":{"url":"http:\/\/shop60857247.taobao.com","account":"\u604b\u821e_2009","password":""},"last_download_time":"1311859242","last_upload_time":null,"active":"true","disabled":"false","last_store_sync_time":null,"area":"mainland:\u5e7f\u4e1c\/\u6df1\u5733\u5e02\/\u7f57\u6e56\u533a:525","zip":"518000","addr":"\u4e1c\u95e8\u4e2d\u8def \u5357\u6d77\u4e2d\u5fc3 B\u5ea7 9E","default_sender":"\u590f\u82b1\u821e\u670d\u9970Dance","mobile":"13715110055","tel":"82180349","filter_bn":"false","bn_regular":null,"express_remark":null,"delivery_template":null,"order_bland_template":null,"node_id":"","node_type":"taobao","addon":{"session":"40726166d5e25d5226b401ewC1ZGyJZfbd3a4e6fb9a97352748748781","nickname":"\u604b\u821e_2009"}}]', true);
$dbo = kernel::database();

foreach ((array) $shopList as $shop) {
	  var_dump($shop);
	if ($shop['addon']['session']) {

		$orderObj->setSessionKey($shop['addon']['session']);
		$orders = $orderObj->fetch($sDate, $eDate);

		echo "There are   {" .count($orders)."} orders\n";
		// var_dump($orders);
		foreach ($orders as $tid) {
			//echo strval($tid);
			$tid = sprintf('%0.0f',$tid);
			$tsql="select order_bn from sdb_ome_orders where order_bn='$tid'";
			$orderbn = $dbo->selectrow($tsql);
			//echo $orderbn;
			if(!$orderbn){
				$orderOme = $orderObj->getFullTrade($tid);

				//var_dump($orderOme);
				//echo "{$tid} \n";
				if (!empty($orderOme)) {

					if(insertIntoOrder($orderOme, $shop['node_id'])){
						echo "{$tid} done.\n";

					}else
					{
						echo "{$tid} fail.\n";
					}
				}
			}else{
				echo "{$tid} has finished.\n";				
			}
				


		}
	}else{
echo '未授权';
	}
}

function insertIntoOrder($sdf, $nodeId) {

	static $orderObj = null;
	static $response = null;

	if (!$orderObj) {
		$orderObj = new ome_rpc_response_order();
		$response = kernel::single('base_rpc_service');
	}

	base_rpc_service::$node_id = $nodeId;

	return $orderObj->add($sdf, $response);
}

function getShopList() {

	$db = kernel::database();
	$list = $db->select("SELECT * FROM sdb_ome_shop WHERE shop_type='taobao' and disabled='false'");
	if ($list) {
		foreach($list as $key => $item) {

			$list[$key]['addon'] = unserialize($item['addon']);
			$list[$key]['config'] = unserialize($item['config']);
		}
		return $list;
	} else {

		return array();
	}
}